package com.lly.songhigh.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lly.songhigh.R;
import com.lly.songhigh.entity.RecommendRoot;

import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by dllo on 15/10/21.
 */
public class FlippableStackViewAdapter extends PagerAdapter {
    private Context context;
    private List<RecommendRoot.DataEntity> dataEntityArrayList;
    private ImageView recommend_img;
    private TextView reason, recommend_name, desc, recommend_position, recommend_size;
    private CardView recommend_cardview;
    private int position;
    private ClickToSkipListener clickToSkipListener;

    public FlippableStackViewAdapter(Context c, List<RecommendRoot.DataEntity> dataEntityArrayList) {
        this.context = c;
        this.dataEntityArrayList = dataEntityArrayList;
    }

    // 点击跳转接口
    public interface ClickToSkipListener {
        void onClickToSkip(RecommendRoot.DataEntity dataEntity);
    }

    public void setClickToSkipListener(ClickToSkipListener clickToSkipListener) {
        this.clickToSkipListener = clickToSkipListener;
    }

    @Override
    public int getCount() {
        return dataEntityArrayList == null ? 0 : dataEntityArrayList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.stack_viewpager_item, null);
        recommend_img = (ImageView) view.findViewById(R.id.recommend_img);
        reason = (TextView) view.findViewById(R.id.reason);
        recommend_name = (TextView) view.findViewById(R.id.recommend_name);
        desc = (TextView) view.findViewById(R.id.desc);
        recommend_position = (TextView) view.findViewById(R.id.recommend_position);
        recommend_size = (TextView) view.findViewById(R.id.recommend_size);
        recommend_cardview = (CardView) view.findViewById(R.id.recommend_cardview);
        //跳转
        recommend_cardview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickToSkipListener != null) {
                    clickToSkipListener.onClickToSkip(getData());
                }
            }
        });
        //设置textview
        reason.setText(dataEntityArrayList.get(position).getReason());
        recommend_name.setText(dataEntityArrayList.get(position).getName());
        desc.setText(dataEntityArrayList.get(position).getDesc());
        recommend_position.setText(String.valueOf(dataEntityArrayList.size() - position));
        recommend_size.setText(String.valueOf(dataEntityArrayList.size()));
        //设置图片
        String picUrl = dataEntityArrayList.get(position).getPicUrl() != null ?
                dataEntityArrayList.get(position).getPicUrl() :
                dataEntityArrayList.get(position).getSmallPicUrls().get(0);
        Picasso.with(context).load(picUrl).into(recommend_img);
        container.addView(view);
        return view;
    }

    public RecommendRoot.DataEntity getData(){
        return dataEntityArrayList.get(position);
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
        this.position = position;
    }
}
